#!/bin/bash
#set -x
#set -e

source ./config/config
source ./script/lib.sh

echo -e "\033[93m|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|"
echo -e "|                        mariadb                     |"
echo -e "|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\033[0m"


mariadb_master_running=$(kubectl get pod -n ${NAMESPACE} | grep ${MARIADB_MASTER_NAME} | grep Running | grep 2/2)
if [[ $? -eq 0 ]]; then
   mariadb_master_running=true
fi

mariadb_slave_running=$(kubectl get pod -n ${NAMESPACE} | grep ${MARIADB_SLAVE_NAME} | grep Running | grep 2/2)
if [[ $? -eq 0 ]]; then
   mariadb_slave_running=true
fi

echo -e "#################  mariadb健康状态  #################"
# 查询主库状态
if [[ $mariadb_master_running == true ]]; then
  mariadb_master_status=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show master status\G")
  echo -e "show master status：" >>$result_dir/mariadb_master_status_detail
  wDetail "$mariadb_master_status\n" mariadb_master_status

  echo -e "---------------------\n" >>$result_dir/mariadb_master_status_detail

  # 查询主库状态
  mariadb_master_status=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G")
  echo -e "show slave status：" >>$result_dir/mariadb_master_status_detail

  mariadb_master_Slave_IO_Running_status=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G" | grep Slave_IO_Running | sed 's/ //g' | awk -F: '{print $2}')
  mariadb_master_Slave_SQL_Running_status=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G" | grep Slave_SQL_Running | sed 's/ //g' | awk -F: '{print $2}')
  if [[ $mariadb_master_Slave_IO_Running_status = "No" || $mariadb_master_Slave_SQL_Running_status = "No" ]];then
  	wResult "FATAL" mariadb_master_status
  else
  	wResult "NORMAL" mariadb_master_status
  fi
  wDetail "$mariadb_master_status\n" mariadb_master_status
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_master_status
fi

################################################################################################################################################################

# 查询从库状态
if [[ $mariadb_slave_running == true ]]; then
  mariadb_slave_status=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show master status\G")
  echo -e "show master status：" >>$result_dir/mariadb_slave_status_detail
  wDetail "$mariadb_slave_status\n" mariadb_slave_status

  echo -e "---------------------\n" >>$result_dir/mariadb_slave_status_detail

  # 查询从库状态
  mariadb_slave_status=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G")
  echo -e "show slave status：" >>$result_dir/mariadb_slave_status_detail

  mariadb_slave_Slave_IO_Running_status=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G" | grep Slave_IO_Running | sed 's/ //g' | awk -F: '{print $2}')
  mariadb_slave_Slave_SQL_Running_status=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "show slave status\G" | grep Slave_SQL_Running | sed 's/ //g' | awk -F: '{print $2}')
  if [[ $mariadb_slave_Slave_IO_Running_status = "No" || $mariadb_slave_Slave_SQL_Running_status = "No" ]];then
  	wResult "FATAL" mariadb_slave_status
  else
  	wResult "NORMAL" mariadb_slave_status
  fi
  wDetail "$mariadb_slave_status\n" mariadb_slave_status
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_slave_status
fi

echo -e "#################  近期日志检查  #################"
# 查询主库状态
if [[ $mariadb_master_running == true ]]; then
  mariadb_master_logs=$(kubectl logs ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} | tail -n 20)
  wDetail "$mariadb_master_logs\n" mariadb_master_logs
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_master_logs
fi

################################################################################################################################################################

# 查询从库状态
if [[ $mariadb_slave_running == true ]]; then
  mariadb_slave_logs=$(kubectl logs ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} | tail -n 20)
  wDetail "$mariadb_slave_logs\n" mariadb_slave_logs
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_slave_logs
fi

echo -e "#################  数据库版本信息检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_version=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT VERSION();")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_version_detail
  wDetail "$mariadb_version\n" mariadb_version
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_version
fi

echo -e "---------------------\n" >>$result_dir/mariadb_version_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_version=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT VERSION();")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_version_detail
  wDetail "$mariadb_version\n" mariadb_version
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_version
fi

echo -e "#################  数据库信息检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_messages=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT table_schema AS 'Database', ENGINE,ROW_FORMAT,TABLE_COLLATION , AUTO_INCREMENT, CREATE_TIME, UPDATE_TIME FROM information_schema.tables GROUP BY table_schema;" | column -t)
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_messages_detail
  wDetail "$mariadb_messages\n" mariadb_messages
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_messages
fi

echo -e "---------------------\n" >>$result_dir/mariadb_messages_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_messages=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT table_schema AS 'Database', ENGINE,ROW_FORMAT,TABLE_COLLATION , AUTO_INCREMENT, CREATE_TIME, UPDATE_TIME FROM information_schema.tables GROUP BY table_schema;" | column -t)
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_messages_detail
  wDetail "mariadb_messages\n" mariadb_messages
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_messages
fi

echo -e "#################  数据库连接检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_connection=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW STATUS LIKE 'Threads_connected';" | column -t -s "|")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n\n" >>$result_dir/mariadb_connection_detail
  echo -e "${MARIADB_MASTER_NAME}总连接数量：\n" >>$result_dir/mariadb_connection_detail
  wDetail "$mariadb_connection\n" mariadb_connection

  echo -e "\n" >>$result_dir/mariadb_connection_detail
  echo -e "${MARIADB_MASTER_NAME}具体连接如下：\n" >>$result_dir/mariadb_connection_detail
  mariadb_connection=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW PROCESSLIST; | column -t")
  wDetail "$mariadb_connection\n" mariadb_connection
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_connection
fi

echo -e "---------------------\n" >>$result_dir/mariadb_connection_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_connection=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW STATUS LIKE 'Threads_connected';" | column -t -s "|")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_connection_detail
  echo -e "${MARIADB_SLAVE_NAME}总连接数量：\n" >>$result_dir/mariadb_connection_detail
  wDetail "$mariadb_connection\n" mariadb_connection

  echo -e "\n" >>$result_dir/mariadb_connection_detail
  echo -e "${MARIADB_SLAVE_NAME}具体连接如下：\n" >>$result_dir/mariadb_connection_detail
  mariadb_connection=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW PROCESSLIST; | column -t")
  wDetail "$mariadb_connection\n" mariadb_connection
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_connection
fi

echo -e "#################  慢sql检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_slowsql=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'slow_query_log%';")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n\n" >>$result_dir/mariadb_slowsql_detail
  echo -e "${MARIADB_MASTER_NAME}慢sql日志位置如下：\n" >>$result_dir/mariadb_slowsql_detail
  wDetail "$mariadb_slowsql\n" mariadb_slowsql

  echo -e "${MARIADB_MASTER_NAME}具体慢sql日志如下：\n" >>$result_dir/mariadb_slowsql_detail

  mariadb_slowsql=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysqldumpslow -s at -t 10 /bitnami/mariadb/logs/slow_query.log)
  wDetail "$mariadb_slowsql\n" mariadb_slowsql
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_slowsql
fi

echo -e "---------------------\n" >>$result_dir/$mariadb_slowsql_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_slowsql=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'slow_query_log%';")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n\n" >>$result_dir/mariadb_slowsql_detail
  echo -e "${MARIADB_SLAVE_NAME}慢sql日志位置如下：\n" >>$result_dir/mariadb_slowsql_detail
  wDetail "$mariadb_slowsql\n" mariadb_slowsql

  echo -e "${MARIADB_SLAVE_NAME}具体慢sql日志如下：\n" >>$result_dir/mariadb_slowsql_detail

  mariadb_slowsql=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysqldumpslow -s at -t 10 /bitnami/mariadb/logs/slow_query.log)
  wDetail "$mariadb_slowsql\n" mariadb_slowsql
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_slowsql
fi

echo -e "#################  配置文件检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_configmap=$(kubectl get cm ${MARIADB_MASTER_NAME} -n cloud-infra -o json  | jq -r '.data.["my.cnf"]')
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_configmap_detail
  wDetail "$mariadb_configmap\n" mariadb_configmap
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_configmap
fi

echo -e "---------------------\n" >>$result_dir/mariadb_configmap_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_configmap=$(kubectl get cm ${MARIADB_SLAVE_NAME} -n cloud-infra -o json  | jq -r '.data.["my.cnf"]')
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_configmap_detail
  wDetail "$mariadb_configmap\n" mariadb_configmap
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_configmap
fi

echo -e "#################  审计日志参数检查  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_general_log=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'general_log';")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_general_log_detail
  wDetail "$mariadb_general_log\n" mariadb_general_log
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_general_log
fi

echo -e "---------------------\n" >>$result_dir/mariadb_general_log_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_general_log=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'general_log';")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_general_log_detail
  wDetail "$mariadb_general_log\n" mariadb_general_log
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_general_log
fi

echo -e "#################  数据库容量检查  #################"
#mariadb_capacity=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT table_schema AS 'Database', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema;")
mariadb_capacity=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT
                                                                                                                                       TABLE_SCHEMA AS database_name,
                                                                                                                                       SUM(TABLE_ROWS) AS total_rows,
                                                                                                                                       ROUND(SUM(DATA_LENGTH) / 1024 / 1024 / 1024, 2) AS total_data_size_gb,
                                                                                                                                       ROUND(SUM(INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_index_size_gb,
                                                                                                                                       ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_size_gb
                                                                                                                                   FROM
                                                                                                                                       information_schema.tables
                                                                                                                                   WHERE
                                                                                                                                       TABLE_SCHEMA NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
                                                                                                                                   GROUP BY
                                                                                                                                       TABLE_SCHEMA
                                                                                                                                   ORDER BY
                                                                                                                                       total_size_gb DESC;" | column -t)

if [[ $mariadb_master_running == true ]]; then
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_capacity_detail
  wDetail "$mariadb_capacity\n" mariadb_capacity
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_capacity
fi

echo -e "---------------------\n" >>$result_dir/mariadb_capacity_detail


if [[ $mariadb_slave_running == true ]]; then
  mariadb_capacity=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT
                                                                                                                                         TABLE_SCHEMA AS database_name,
                                                                                                                                         SUM(TABLE_ROWS) AS total_rows,
                                                                                                                                         ROUND(SUM(DATA_LENGTH) / 1024 / 1024 / 1024, 2) AS total_data_size_gb,
                                                                                                                                         ROUND(SUM(INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_index_size_gb,
                                                                                                                                         ROUND(SUM(DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_size_gb
                                                                                                                                     FROM
                                                                                                                                         information_schema.tables
                                                                                                                                     WHERE
                                                                                                                                         TABLE_SCHEMA NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
                                                                                                                                     GROUP BY
                                                                                                                                         TABLE_SCHEMA
                                                                                                                                     ORDER BY
                                                                                                                                         total_size_gb DESC;" | column -t)
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_capacity_detail
  wDetail "$mariadb_capacity\n" mariadb_capacity
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_capacity
fi

echo -e "#################  数据库表碎片  #################"

if [[ $mariadb_master_running == true ]]; then
    mariadb_fragments=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT
                                                                                                                                            TABLE_SCHEMA AS database_name,
                                                                                                                                            TABLE_NAME AS table_name,
                                                                                                                                            ENGINE AS storage_engine,
                                                                                                                                            ROUND(DATA_LENGTH / 1024 / 1024 / 1024, 2) AS data_size_gb,
                                                                                                                                            ROUND(INDEX_LENGTH / 1024 / 1024 / 1024, 2) AS index_size_gb,
                                                                                                                                            ROUND(DATA_FREE / 1024 / 1024 / 1024, 2) AS free_space_gb,
                                                                                                                                            ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_size_gb,
                                                                                                                                            ROUND(DATA_FREE / (DATA_LENGTH + INDEX_LENGTH) * 100, 2) AS fragmentation_percentage
                                                                                                                                        FROM
                                                                                                                                            information_schema.tables
                                                                                                                                        WHERE
                                                                                                                                            TABLE_SCHEMA NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
                                                                                                                                            AND DATA_FREE > 0
                                                                                                                                        ORDER BY
                                                                                                                                            free_space_gb DESC;" | column -t)

  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_fragments_detail
  wDetail "$mariadb_fragments\n" mariadb_fragments
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_fragments
fi

echo -e "---------------------\n" >>$result_dir/mariadb_fragments_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_fragments=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT
                                                                                                                                          TABLE_SCHEMA AS database_name,
                                                                                                                                          TABLE_NAME AS table_name,
                                                                                                                                          ENGINE AS storage_engine,
                                                                                                                                          ROUND(DATA_LENGTH / 1024 / 1024 / 1024, 2) AS data_size_gb,
                                                                                                                                          ROUND(INDEX_LENGTH / 1024 / 1024 / 1024, 2) AS index_size_gb,
                                                                                                                                          ROUND(DATA_FREE / 1024 / 1024 / 1024, 2) AS free_space_gb,
                                                                                                                                          ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024 / 1024, 2) AS total_size_gb,
                                                                                                                                          ROUND(DATA_FREE / (DATA_LENGTH + INDEX_LENGTH) * 100, 2) AS fragmentation_percentage
                                                                                                                                      FROM
                                                                                                                                          information_schema.tables
                                                                                                                                      WHERE
                                                                                                                                          TABLE_SCHEMA NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
                                                                                                                                          AND DATA_FREE > 0
                                                                                                                                      ORDER BY
                                                                                                                                          free_space_gb DESC;" | column -t)

  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_fragments_detail
  wDetail "$mariadb_fragments\n" mariadb_fragments
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_fragments
fi

echo -e "#################  数据库长事务  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_transaction=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT * FROM information_schema.innodb_trx;")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_transaction_detail
  wDetail "$mariadb_transaction\n" mariadb_transaction
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_transaction
fi

echo -e "---------------------\n" >>$result_dir/mariadb_transaction_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_transaction=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SELECT * FROM information_schema.innodb_trx;")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_transaction_detail
  wDetail "$mariadb_transaction\n" mariadb_transaction
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_transaction
fi

echo -e "#################  数据库缓存大小  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_cache_size=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'query_cache_size';" | column -t -s "|")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_cache_size_detail
  wDetail "$mariadb_cache_size\n" mariadb_cache_size
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_cache_size
fi

echo -e "---------------------\n" >>$result_dir/mariadb_cache_size_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_cache_size=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW VARIABLES LIKE 'query_cache_size';" | column -t -s "|")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_cache_size_detail
  wDetail "$mariadb_cache_size\n" mariadb_cache_size
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_cache_size
fi

echo -e "#################  数据库缓存命中率  #################"
if [[ $mariadb_master_running == true ]]; then
  mariadb_qcache=$(kubectl exec -it ${MARIADB_MASTER_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW STATUS LIKE 'Qcache%';" | column -t -s "|")
  echo -e "${MARIADB_MASTER_NAME}输出如下：\n" >>$result_dir/mariadb_qcache_detail
  wDetail "$mariadb_qcache\n" mariadb_qcache
else
  wDetail "${MARIADB_MASTER_NAME}服务没启动\n" mariadb_qcache
fi

echo -e "---------------------\n" >>$result_dir/mariadb_qcache_detail

if [[ $mariadb_slave_running == true ]]; then
  mariadb_qcache=$(kubectl exec -it ${MARIADB_SLAVE_NAME}-0 -c mariadb -n ${NAMESPACE} -- mysql -u root -p${MARIADB_PASSWORD} -e "SHOW STATUS LIKE 'Qcache%';" | column -t -s "|")
  echo -e "${MARIADB_SLAVE_NAME}输出如下：\n" >>$result_dir/mariadb_qcache_detail
  wDetail "$mariadb_qcache\n" mariadb_qcache
else
  wDetail "${MARIADB_SLAVE_NAME}服务没启动\n" mariadb_qcache
fi